<?php
	require_once($_SERVER['DOCUMENT_ROOT'].'uno-course-scheduler/include/config.inc.php');
	require_once($_SERVER['DOCUMENT_ROOT'].'uno-course-scheduler/include/top.inc.php');

$section_id = mysql_real_escape_string(strip_tags($_GET['sec_id']));
$query = "SELECT *, csm.id AS csm_id, c.id AS cid, csub.id AS sid
 		  FROM course_sections cs
		  JOIN course_section_mapping csm ON csm.course_section_id = cs.id
		  JOIN courses c ON c.id = csm.course_id
		  JOIN course_subjects csub ON csub.id = c.course_subject_id
		  WHERE cs.id = '$section_id'";
$result = mysql_query($query);
$row=mysql_fetch_assoc($result);
$csm_id = $row['csm_id'];
$course_id = $row['cid'];
$subject_id = $row['sid'];

$get_subject_data = mysql_query("SELECT * FROM course_subjects WHERE id = $subject_id");
$subject_name = mysql_result($get_subject_data,0,'subject_title');
$abbreviation = mysql_result($get_subject_data,0,'abbreviation');

$get_course_data = mysql_query("SELECT * FROM courses WHERE id = $course_id");
$course_number = mysql_result($get_course_data,0,'course_number');
$course_name = mysql_result($get_course_data,0,'title');

$get_section_data = mysql_query("SELECT * FROM course_sections WHERE id = $section_id");
$section_number = mysql_result($get_section_data,0,'section_number');

//counting variable
$i = 0;

if (isset($_GET['p_up']))
{
	$mid_up = $_GET['mid'];
	
	$move_up=mysql_query("SELECT * FROM course_section_mapping WHERE id = $mid_up");
	
	$priority=mysql_result($move_up,0,"scheduling_rank");
	$pri_down = $priority;
	$pri_up = $priority - 1;
	
	$move_down=mysql_query("SELECT * FROM course_section_mapping WHERE scheduling_rank = '$pri_up' AND course_section_id = '$section_id'");

	$mid_down=mysql_result($move_down,0,"id");
	
	mysql_query("UPDATE course_section_mapping 
		SET scheduling_rank='$pri_up'
		WHERE id='$mid_up'");

	mysql_query("UPDATE course_section_mapping 
		SET scheduling_rank='$pri_down'
		WHERE id='$mid_down'");
	
}

if (isset($_GET['p_down']))
{
	$mid_down = $_GET['mid'];
	
	$move_down=mysql_query("SELECT * FROM course_section_mapping WHERE id = $mid_down");
	
	$priority=mysql_result($move_down,0,"scheduling_rank");
	$pri_up = $priority;
	$pri_down = $priority + 1;
	
	$move_up=mysql_query("SELECT * FROM course_section_mapping WHERE scheduling_rank = '$pri_down' AND course_section_id = '$section_id'");

	$mid_up=mysql_result($move_up,0,"id");
	
	mysql_query("UPDATE course_section_mapping 
		SET scheduling_rank='$pri_up'
		WHERE id='$mid_up'");

	mysql_query("UPDATE course_section_mapping 
		SET scheduling_rank='$pri_down'
		WHERE id='$mid_down'");
}

//select statement
$query="SELECT * FROM course_section_mapping WHERE course_section_id = $section_id ORDER BY scheduling_rank";
$result=mysql_query($query);

$find_insert=mysql_query("SELECT * FROM course_section_mapping");
$insert_point=mysql_numrows($find_insert);

//add will be done at one past last entry currently in table
$insert_point++;

//determine dynamically number of rows read from table
$num_rows=mysql_numrows($result);

/*
$query2="SELECT * FROM course_subjects where id = $subject_id";
$result2=mysql_query($query2);

$subject_name=mysql_result($result2,$i,"subject_title");
$abbreviation=mysql_result($result2,$i,"abbreviation");
*/

$formatted_section_number = sprintf("%03d", $section_number);

echo "<p>";
	echo "<a href='../index.php?sid=$subject_id&id=$course_id'>&lt;&lt; Back to Sections Portal</a><br />";
echo "</p>";
echo "<h1>Crosslistings for $abbreviation $course_number - $formatted_section_number</h1>";
echo '<p>';
//creates link/button for add crosslisting
echo "<a href='add.php?sec_id=$section_id'>Add Crosslisting</a>";
echo '</p>';

if ($num_rows > 0)
{
	echo '<p>';

	//start table
	echo "<table class=\"postSchedule\">";
	
		//create table row with column ID's
		echo "<tr>";
			echo "<td><b>Subject</b></td>";
			echo "<td width='80' style='text-align: center'><b>Scheduling Rank</b></td>";
			echo "<td></td>";
			echo "<td></td>";
			echo "<td></td>";
		echo "</tr>";

	//loop to walk through and print to screen all entries in timeslots table
	$row_count=0;
	while ($i < $num_rows) 
	{
		$row_count++;
		$cl_csm_id = mysql_result($result,$i,"id");
		$cl_course_id = mysql_result($result,$i,"course_id");
		$cl_rank = mysql_result($result,$i,"scheduling_rank");
		
		$cl_get_course_data = mysql_query("SELECT * FROM courses WHERE id = $cl_course_id");
		$cl_course_number = mysql_result($cl_get_course_data,0,'course_number');
		$cl_course_name = mysql_result($cl_get_course_data,0,'title');
		$cl_subject_id = mysql_result($cl_get_course_data,0,'course_subject_id');
		
		$cl_get_subject_data = mysql_query("SELECT * FROM course_subjects WHERE id = $cl_subject_id");
		$cl_subject_name = mysql_result($cl_get_subject_data,0,'subject_title');
		$cl_abbreviation = mysql_result($cl_get_subject_data,0,'abbreviation');
		
		//generate table row for each mapping on each iteration of the loop
		echo "<tr";
		if (($row_count%2) != 0) echo " class=\"zebra_stripe\"";
		echo ">";
			echo "<td>$cl_abbreviation $cl_course_number - $cl_course_name</td>";	
			echo "<td style='text-align: center'>$cl_rank</td>";		

			if (!($i == 0))
			{
				echo "<td>";		//cell containing form for priority up
					echo "<a href='index.php?cid=$course_id&amp;sid=$subject_id&amp;csm_id=$csm_id&amp;sec_id=$section_id&amp;mid=$cl_csm_id&amp;p_up=Priority+Up'>Priority Up</a>";
				echo "</td>";
			}
			else
			{
				echo "<td></td>";
			}
			
			if (!($i ==$num_rows-1))
			{
				echo "<td>";		//cell containing form for priority up
					echo "<a href='index.php?cid=$course_id&amp;sid=$subject_id&amp;csm_id=$csm_id&amp;sec_id=$section_id&amp;mid=$cl_csm_id&amp;p_down=Priority+Down'>Priority Down</Down>";
				echo "</td>";
			}
			else
			{
				echo "<td></td>";
			}
			if ($num_rows > 1)
			{
				echo "<td>";		//cell containing form for delete mapping link/button
					echo "<a href='delete.php?cid=$course_id&amp;sid=$subject_id&amp;csm_id=$csm_id&amp;sec_id=$section_id&amp;id=$cl_csm_id'>Delete</a>";
				echo "</td>";
			}
	
		echo "</tr>";
		
		//increment counting variable
		$i++;
	}

	//end table tag
	echo "</table>";

	echo '</p>';

}
else
{
	echo "<p><b>NO COURSES ASSOCIATED WITH SECTION $section_number</b></p>";
}



?>

<?php
	require_once($_SERVER['DOCUMENT_ROOT'].'uno-course-scheduler/include/bottom.inc.php');
?>